var amapFile = require('../../utils/amap-wx.js')
Page({
  data: {
    markers: [],
    distance: '',
    cost: '',
    polyline: [],
    info:{}
  },
  onLoad: function (options) {
    let info=JSON.parse(options.info)
    var that = this;
    wx.getLocation({
      type: 'wgs84',
      success (res) {
        that.setData({
          latitude:res.latitude,
          longitude:res.longitude,
        })
      }
     })
    that.setData({
      info:info
    })
    that.map()
   
  },
  map(){
    let that=this
    let markers = that.data.markers
    let info=that.data.info
    markers.push({
      id: 0,
      longitude: Number(info.staff.longitude),
      latitude: Number(info.staff.latitude),
      iconPath: '/img/start.png',
      width: 40,
      height: 40,
    },{
      id: 1,
      iconPath: '/img/end.png',
      longitude: Number(info.longitude),
      latitude:  Number(info.latitude),
      width: 40,
      height: 40,
    });
    that.setData({
      markers:markers
    })
    var myAmapFun = new amapFile.AMapWX({
      key: 'f8b1262c6afbe526f37c449e593ea859'
    });
    var ids=[]
    ids.push(that.data.markers[0].longitude)
    ids.push(that.data.markers[0].latitude)
    var ids1=[]
    ids1.push(that.data.markers[1].longitude)
    ids1.push(that.data.markers[1].latitude)
    var userIdsStr =String(ids.join(','));
    var userIdsStr1 =String(ids1.join(','));
    myAmapFun.getDrivingRoute({
      origin: userIdsStr,
      destination:userIdsStr1,
      success: function (data) {
        var points = [];
        if (data.paths && data.paths[0] && data.paths[0].steps) {
          var steps = data.paths[0].steps;
          for (var i = 0; i < steps.length; i++) {
            var poLen = steps[i].polyline.split(';');
            for (var j = 0; j < poLen.length; j++) {
              points.push({
                longitude: parseFloat(poLen[j].split(',')[0]),
                latitude: parseFloat(poLen[j].split(',')[1])
              })
            }
          }
        }
        that.setData({
          polyline: [{
            points: points,
            color: "#0091ff",
            width: 6
          }]
        });
        if (data.paths[0] && data.paths[0].distance) {
          that.setData({
            distance: ((data.paths[0].distance) / 1000).toFixed(2) + 'km'
          });
        }
      },
      fail: function (info) {

      }
    })
  },
  getlocation() {
    let that = this
    wx.getSetting({
      success: (res) => {
        var statu = res.authSetting;
        if (!statu['scope.userLocation']) {
          wx.showModal({
            title: '是否授权当前位置',
            content: '需要获取您的地理位置，请确认授权，否则地图功能将无法使用',
            success: function (tip) {
              if (tip.confirm) {
                wx.openSetting({
                  success (res) {
                  }
                })
              }
            }
          })
        }
      }
    })
  },
  onReady: function () {

  },
  onShow: function () {

  },
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})